{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b66a2c97-4c52-4bd4-9afe-df4eaf359c0c",
   "metadata": {},
   "source": [
    "# PaddleHub模型空间的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1c6e6eb-3007-4bc6-96f1-45a044d28894",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-04-16T08:58:33.279783Z",
     "iopub.status.busy": "2024-04-16T08:58:33.279273Z",
     "iopub.status.idle": "2024-04-16T08:58:33.348155Z",
     "shell.execute_reply": "2024-04-16T08:58:33.347257Z",
     "shell.execute_reply.started": "2024-04-16T08:58:33.279749Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 创建模型空间，上传自己的模型\n",
    "import os\n",
    "from aistudio_sdk.hub import create_repo, upload, download\n",
    "\n",
    "os.environ[\"AISTUDIO_ACCESS_TOKEN\"] = \"******\"\n",
    "repo_id = \"*****\"\n",
    "\n",
    "\n",
    "def get_fp(root, filter=\"check\"):\n",
    "    \"\"\"\n",
    "    获取目录下所有文件路径\n",
    "    Args:\n",
    "        root: 指定目录\n",
    "        filter: 不期望的条件\n",
    "\n",
    "    Example:\n",
    "        import os\n",
    "        root = \"Animagine-xl-2.0\"\n",
    "        fp = get_fp(root, filter=\"check\")\n",
    "        fp\n",
    "    \"\"\"\n",
    "    fp=[]\n",
    "    for rs, _, fs in os.walk(root):\n",
    "        if filter not in rs:\n",
    "            for f in fs:\n",
    "                fp.append(os.path.join(rs, f))\n",
    "    return fp\n",
    "\n",
    "\n",
    "res = create_repo(\n",
    "    repo_id='myrepoid',\n",
    "    model_name='mymodelname',\n",
    "    desc='mydesc',\n",
    "    private=False,\n",
    "    license='Apache License 2.0'\n",
    ")\n",
    "print(res)\n",
    "\n",
    "\n",
    "# 上传文件夹示例\n",
    "root = \"IP-Adpater\"\n",
    "fp = get_fp(root, filter=\"check\")\n",
    "for f in fp:\n",
    "    res = upload(\n",
    "        repo_id=repo_id,\n",
    "        path_or_fileobj=f,\n",
    "        path_in_repo=f\n",
    "    )\n",
    "print(res)\n",
    "\n",
    "# 上传文件示例\n",
    "f = \"animagine_demo.ipynb\"\n",
    "res = upload(\n",
    "    repo_id=repo_id,\n",
    "    path_or_fileobj=f,\n",
    "    path_in_repo=f\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
